import Cookies from "js-cookie"

const state = {
  systemName: localStorage.getItem("systemName") || "baomihuaxiaobu",
  openSidebar: Cookies.get("sidebarStatus") === "0" ? false : true, //true 展开  false 收起
  device: "desktop",
  showLoading: false, //展示全局加载动画
  screenHeight: null // 屏幕高度
}

const mutations = {
  // 设置系统名字
  SET_SYSTEM_NAME: (state, systemName) => {
    state.systemName = systemName
  },
  //设置请求动画状态
  SET_LOADING_STATUS: (state, showLoading) => {
    state.showLoading = showLoading
  },
  //切换侧边栏展开或收起
  TOGGLE_SIDEBAR: state => {
    state.openSidebar = !state.openSidebar
    if (state.openSidebar) {
      Cookies.set("sidebarStatus", 1) //1 展开
    } else {
      Cookies.set("sidebarStatus", 0) //0 收起
    }
  },
  //收起侧边栏
  CLOSE_SIDEBAR: (state) => {
    Cookies.set("sidebarStatus", 0)
    state.openSidebar = false
  },
  //切换设备
  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  },
  // 设置屏幕高度
  SET_SCREEN_HEIGHT: (state, screenHeight) => {
    state.screenHeight = screenHeight
  }
}

const actions = {
  /**
   * @description: 切换侧边菜单栏状态
   * @param {type}
   * @return {type}
   * @author: syx
   */
  toggleSideBar({ commit }) {
    commit("TOGGLE_SIDEBAR")
  },
  /**
   * @description: 关闭侧边菜单栏
   * @param {type}
   * @return {type}
   * @author: syx
   */
  closeSideBar({ commit }) {
    commit("CLOSE_SIDEBAR")
  },
  /**
   * @description: 切换设备
   * @param {type}
   * @return {type}
   * @author: syx
   */
  toggleDevice({ commit }, device) {
    commit("TOGGLE_DEVICE", device)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
